package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.core.service.ServiceService;
import com.sz.biz.logistics.core.dto.ServiceDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.core.service.PrincipalUtils;
import com.sz.app.oss.api.logistics.base.dto.ApprovalAuditMsgDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * Function: ServiceController <br>
 * Author: wentao.chang <br>
 * Date: 2016-11-28 14:20:00
 */
@RestController
@RequestMapping("${api}/biz/services")
@Api(description = " ", tags = "032-1、服务审核管理")
public class ServiceApprovalController extends BaseController {
    @Autowired
    private ServiceService serviceService;

    @ApiOperation(value = "修改服务", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SERVICE)
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "uid", value = "修改服务的uid", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "serviceDto", value = "提交的服务信息\r\n "
                    + "同新增", required = true, dataType = "ServiceDto", paramType = "body")})
    @RequestMapping(value = "/{uid}/approval", method = RequestMethod.PUT)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL})
    public ResultDto updateServiceMain(@PathVariable String uid, @RequestBody ServiceDto serviceDto) throws ParseException {
        serviceDto.setLastUpdateUserId(PrincipalUtils.getAccountId());
        serviceService.update(uid, serviceDto,true);
        ResultDto resultDto = getUpdateSuccessDto(MessagesCodes.MODULE_SERVICE);
        resultDto.setData(uid);
        return resultDto;
    }


    @ApiOperation(value = "审核服务", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "服务uid", dataType = "String",
                    paramType = "path", required = true),
            @ApiImplicitParam(name = "msg", value = "审核意见", dataType = "ApprovalAuditMsgDto",
                    paramType = "body", required = true)
    })
    @RequestMapping(value = "{uid}/audit", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL})
    public ResultDto audit(@PathVariable String uid, @RequestBody ApprovalAuditMsgDto msg) throws ParseException {
        serviceService.audit(Arrays.asList(uid), msg.getMsg());
        ResultDto resultDto = getAuditAndEnableSuccessDto(MessagesCodes.MODULE_SERVICE);
        return resultDto;
    }

    @ApiOperation(value = "打回服务", notes = "打回服务-<br>" + Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "服务uid", dataType = "String", paramType = "path", required = true),
            @ApiImplicitParam(name = "msg", value = "消息", dataType = "ApprovalAuditMsgDto", paramType = "body", required = true)
    })
    @RequestMapping(value = "{uid}/rejections", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL})
    public ResultDto reject(@PathVariable String uid, @RequestBody ApprovalAuditMsgDto msg) throws ParseException {
        serviceService.reject(Arrays.asList(uid), msg.getMsg());
        return getRejectSuccessDto();
    }

    @ApiOperation(value = "按uid上线一个服务", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "服务的uid ", required = true, dataType = "String[]", paramType = "body")})
    @RequestMapping(value = "/online", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL})
    public ResultDto online(@RequestBody List<String> uids) throws ParseException {
        serviceService.online(uids);
        ResultDto resultDto = new ResultDto(0, messageSource.getMessage(MessagesCodes.SERVICE_ONLINE_SUCCESS, null));
        return resultDto;
    }

    @ApiOperation(value = "按uid下线一个服务", notes = "-<br>权限：" + Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uids", value = "服务的uid ", required = true, dataType = "String[]", paramType = "body"),
            @ApiImplicitParam(name = "isconfirmed", value = "是否确认，\r\n" +
                    "第一次传false,确认之后传true", dataType = "boolean",
                    paramType = "query", required = true, defaultValue = "false")})
    @RequestMapping(value = "/offline", method = RequestMethod.POST)
    @RequiresPermissions(value = {Permissions.BIZ_MGT_BASE_SERVICE_APPROVAL})
    public ResultDto offline(@RequestBody List<String> uids,
                             @RequestParam(required = false, defaultValue = "false") boolean isconfirmed
    ) throws ParseException {
        serviceService.offline(uids, isconfirmed);
        ResultDto resultDto = new ResultDto(0, messageSource.getMessage(MessagesCodes.SERVICE_OFFLINE_SUCCESS, null));
        return resultDto;
    }
}
